#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define inf 1e18
#define ll long long
#define db double
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define ft first
#define se second
inline int read(){
    char ch=getchar();
    int x=0,f=1;
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
using namespace std;
#define M 200005
#define N 1000005
int m,n,a[M],lst[N];
ll pre[M],dp[M];
void solve(){
    m=read(),n=0;
    ll ans=0;
    vector<pii> wait;
    wait.pb(mk(0,0));
    for(int i=1;i<=m;i++){
        a[i]=read();
        if(a[i]==a[i-1]) ans+=a[i];
        else{
            a[++n]=a[i];
            if(lst[a[i]]) wait.pb(mk(lst[a[i]],n));
            lst[a[i]]=n;
        }
    }
    for(int i=1;i<=n;i++) lst[a[i]]=0;
    m=wait.size()-1;
    for(int i=1;i<=m;i++){
        int l=0,r=i-1,res=-1,x=wait[i].ft;
        while(l<=r){
            int mid=(l+r)>>1;
            if(wait[mid].se-1<=x) res=mid,l=mid+1;
            else r=mid-1;
        }
        dp[i]=pre[res]+a[x];
        pre[i]=max(pre[i-1],dp[i]);
    }
    printf("%lld\n",ans+(*max_element(dp+1,dp+m+1)));
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T=read();
    while(T--) solve();
    return 0;
}